package top.leix.springdemo.services.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.leix.springdemo.NowSemester;
import top.leix.springdemo.Res;
import top.leix.springdemo.mapper.ClasstableMapper;
import top.leix.springdemo.pojo.ApplyLabTeacher;
import top.leix.springdemo.pojo.Classtable;
import top.leix.springdemo.services.ApplyLabTeacherService;
import top.leix.springdemo.mapper.ApplyLabTeacherMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
* @author 16435
* @description 针对表【apply_lab_teacher(教师申请表)】的数据库操作Service实现
* @createDate 2024-04-21 22:23:42
*/
@Service
public class ApplyLabTeacherServiceImpl extends ServiceImpl<ApplyLabTeacherMapper, ApplyLabTeacher>
    implements ApplyLabTeacherService {
    @Resource
    ApplyLabTeacherMapper applyLabTeacherMapper;
    @Resource
    ClasstableMapper classtableMapper;
    private boolean checkConflict(ApplyLabTeacher applyLabTeacher, String semester) {
        // 构建查询条件
        QueryWrapper<Classtable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("semester", semester);
        queryWrapper.eq("weekDay", applyLabTeacher.getWeekDay());
        queryWrapper.eq("labType", applyLabTeacher.getLabType());
        queryWrapper.apply("startWeek <= {0} AND endWeek >= {0}", applyLabTeacher.getEndWeek());
        queryWrapper.or().apply("startWeek <= {0} AND endWeek >= {0}", applyLabTeacher.getStartWeek());
        // 执行查询
        List<Classtable> conflictClasses = classtableMapper.selectList(queryWrapper);
        return !conflictClasses.isEmpty(); // 如果有冲突的课程，返回true；否则返回false
    }

    @Override
    public Res addApplication(ApplyLabTeacher applyLabTeacher) {

//        String nowSemester = NowSemester.getNowSemester();
//        applyLabTeacher.setSemester(nowSemester);
//        // 检查申请的时段是否已经有课
//        boolean isConflict = checkConflict(applyLabTeacher, nowSemester);
//        if (isConflict) {
//            return Res.failed("申请的时段已有课程，请重新选择时段");
//        }
        applyLabTeacher.setSemester(NowSemester.getNowSemester());//更新为当前学期
        applyLabTeacher.setApplyStatus("Pending");
        int insertCount = applyLabTeacherMapper.insert(applyLabTeacher);
        if (insertCount > 0) {
            return Res.ok("OK");
        } else {
            return Res.failed("添加申请失败");
        }
    }
    @Override
    public Res updateApplication(ApplyLabTeacher applyLabTeacher) {
        int updateCount = applyLabTeacherMapper.updateById(applyLabTeacher);
        if (updateCount > 0) {
            return Res.ok("OK");
        } else {
            return Res.failed("更新申请失败");
        }
    }
    /**
     * 更新处理进度
     * @param status
     * @return
     */
    @Override
    public Res updateApplicationStatus(Integer applyID, String status) {
        int res = applyLabTeacherMapper.updateStatus(applyID,status);
        if (res > 0){
            return Res.ok("OK");
        }
        return Res.failed(null);
    }
    /**
     * 查询所有老师的表单
     */
    @Override
    public Res queryAllTeacherApply() {
        List<ApplyLabTeacher> labList = applyLabTeacherMapper.selectList(new QueryWrapper<>());
        return Res.ok(labList);
    }

    /**
     * 管理员查询未处理老师的表单
     * @return
     */
    @Override
    public Res queryToCheckTeacher() {
        QueryWrapper<ApplyLabTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Pending");
        queryWrapper.eq("semester", NowSemester.getNowSemester());
        List<ApplyLabTeacher> labList = applyLabTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 老师查自己未审核的申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryPending(String userAccount) {
        QueryWrapper<ApplyLabTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Pending");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabTeacher> labList = applyLabTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    /**
     * 老师查自己审核通过的申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryScheduled(String userAccount) {
        QueryWrapper<ApplyLabTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyStatus","Scheduled");
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabTeacher> labList = applyLabTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }
    /**
     * 老师查看自己历史申请
     * @param userAccount
     * @return
     */
    @Override
    public Res queryHistory(String userAccount) {
        QueryWrapper<ApplyLabTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applyUserAccount",userAccount);
        List<ApplyLabTeacher> labList = applyLabTeacherMapper.selectList(queryWrapper);
        return Res.ok(labList);
    }

    @Override
    public Res updateTeacherApplyRejection(Integer applyID) {
        int res = applyLabTeacherMapper.updateTeacherApplyRejection(applyID);
        if(res > 0){
            return Res.ok(null);
        }
        return Res.failed(null);
    }
}




